<template>
  <div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
        <el-form-item label="开启签到" prop="status">
          <el-switch v-model="dataForm.status"
            active-color="#13ce66"
            active-value="1"
            active-text="开启"
            inactive-text="关闭"
            inactive-color="#ff4949"
            inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-divider>连续签到奖励</el-divider>
        <el-form-item label="第一天" prop="points">
          <el-input-number v-model="dataForm.points[0]" placeholder="第一天"></el-input-number>
        </el-form-item>
        <el-form-item label="第二天" prop="points">
          <el-input-number v-model="dataForm.points[1]" placeholder="第二天"></el-input-number>
        </el-form-item>
        <el-form-item label="第三天" prop="points">
          <el-input-number v-model="dataForm.points[2]" placeholder="第三天"></el-input-number>
        </el-form-item>
        <el-form-item label="第四天" prop="points">
          <el-input-number v-model="dataForm.points[3]" placeholder="第四天"></el-input-number>
        </el-form-item>
        <el-form-item label="第五天" prop="points">
          <el-input-number v-model="dataForm.points[4]" placeholder="第五天"></el-input-number>
        </el-form-item>
        <el-form-item label="第六天" prop="points">
          <el-input-number v-model="dataForm.points[5]" placeholder="第六天"></el-input-number>
        </el-form-item>
        <el-form-item label="第七天" prop="points">
          <el-input-number v-model="dataForm.points[6]" placeholder="第七天"></el-input-number>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </div>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        status: 1,
        points: []
      },
      dataRule: {
        appid: [
          {
            required: true,
            message: 'appid不能为空',
            trigger: 'blur'
          }
        ],
        appkey: [
          {
            required: true,
            message: 'appkey不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  activated () {
    this.init ()
  },
  methods: {
    init () {
      this.$http({
        url: `/mall/usersignrecord/config`,
        method: 'get'
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataForm = data.config
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm']
        .validate((valid) => {
          if (valid) {
            this.$http({
              url: `/mall/usersignrecord/saveConfig`,
              method: 'post',
              data: this.dataForm
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.init()
              }
            })
          }
        })
    }
  }
}
</script>
